package com.xauto.pioneer.appuser.activity.mine.user;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xauto.pioneer.appuser.MyApplication;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.CarRefuelDetailInfo;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.view.TopBar;

import butterknife.Bind;

/**
 * Created by Ellis.Li on 2017/5/25.
 */

public class RefuelDetailActivity extends BaseActivity {
    @Bind(R.id.topBar)
    TopBar topBar;
    String car_refuel_id = "";
    CarRefuelDetailInfo carRefuelInfo;
    @Bind(R.id.tvOrderNo)
    TextView tvOrderNo;
    @Bind(R.id.tvCarPlate)
    TextView tvCarPlate;
    @Bind(R.id.tvRefuelDate)
    TextView tvRefuelDate;
    @Bind(R.id.tvRefuelMoney)
    TextView tvRefuelMoney;
    @Bind(R.id.tvRefuelStatus)
    TextView tvRefuelStatus;
    @Bind(R.id.tvRefuelStatusDesc)
    TextView tvRefuelStatusDesc;
    @Bind(R.id.refuel_before_image)
    ImageView refuelBeforeImage;
    @Bind(R.id.refuel_after_image)
    ImageView refuelAfterImage;

    private ImageLoader imageLoader;
    private DisplayImageOptions options;
    public static void startAction(Context context, String car_refuel_id) {
        Intent intent = new Intent(context, RefuelDetailActivity.class);
        intent.putExtra("car_refuel_id", car_refuel_id);
        context.startActivity(intent);
    }

    @Override
    protected int getContentView() {
        return R.layout.ac_refuel_detail;
    }

    @Override
    protected void initView() {
        topBar.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE);
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setText(TopBar.TITLE, "加油详情");
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void initData() {
        imageLoader = ImageLoader.getInstance();
        options = MyApplication.getDisplayImageOptions(mContext, 40, R.mipmap.ic_default_user_avatar);
        car_refuel_id = getIntent().getStringExtra("car_refuel_id");
    }

    @Override
    protected void onStart() {
        super.onStart();
        loadRefuelDetail();
    }

    /**
     * 加载详情信息
     */
    private void loadRefuelDetail() {
        new LHttpLib().getCarRefuelDetail(mContext, memberId, car_refuel_id, new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                carRefuelInfo = new CarRefuelDetailInfo().parse(jsonStatus.data.optJSONObject("car_refuel_info"));
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showRefuelData();
                    }
                });
            }
        });
    }

    /**
     * 显示相关数据
     */
    private void showRefuelData() {
        tvOrderNo.setText(carRefuelInfo.car_order_no);
        tvCarPlate.setText(carRefuelInfo.plate_no);
        //todo 缺少加油日子
        tvRefuelDate.setText(carRefuelInfo.time);
        tvRefuelMoney.setText(carRefuelInfo.car_refuel_amount_foramt);
        tvRefuelStatus.setText(carRefuelInfo.car_refuel_status_name);
        switch (carRefuelInfo.car_refuel_status_style_color){
            case "0":
                tvRefuelStatus.setTextColor(getResources().getColor(R.color.black));
                break;
            case "1":
                tvRefuelStatus.setTextColor(getResources().getColor(R.color.red));
                break;
            case "2":
                tvRefuelStatus.setTextColor(getResources().getColor(R.color.grey));
                break;
        }
        if(carRefuelInfo.car_refuel_status_name.equals("待结算")){
        tvRefuelStatusDesc.setVisibility(View.VISIBLE);}else{
            tvRefuelStatusDesc.setVisibility(View.GONE);
        }
        switch (carRefuelInfo.car_refuel_comment_style_color){
            case "0":
                tvRefuelStatus.setTextColor(getResources().getColor(R.color.black));
                break;
            case "1":
                tvRefuelStatus.setTextColor(getResources().getColor(R.color.red));
                break;
            case "2":
                tvRefuelStatus.setTextColor(getResources().getColor(R.color.grey));
                break;
        }
       if (carRefuelInfo.refuel_image_list != null){
           int size = carRefuelInfo.refuel_image_list.size();
           switch (size){
               case 1:
                   imageLoader.displayImage(carRefuelInfo.refuel_image_list.get(0),refuelBeforeImage,options);
                   break;
               case 2:
                   imageLoader.displayImage(carRefuelInfo.refuel_image_list.get(0),refuelBeforeImage,options);
                   imageLoader.displayImage(carRefuelInfo.refuel_image_list.get(1),refuelAfterImage,options);
                   break;
           }
       }
    }
}
